<?php

namespace app\index\controller;

use Alipay\EasySDK\Kernel\Factory;
use app\common\controller\Payment;
use app\common\lib\Alipay;
use app\common\lib\Put;
use app\common\model\Member;

class Notify extends Common
{


    public function wechat(){
        return (new Payment())->wechat_notify();
    }

    public function alipay(){
        Factory::setOptions(Alipay::getOptions());
        $route = 'notify/alipayOrder.log';
        try {
            $param = $_POST;
            $result = Factory::payment()->common()->verifyNotify($param);
            if ($result) {
                if ($param['trade_status'] != 'TRADE_SUCCESS') {
                    Put::write($route, '非支付回调');
                    exit();
                }
                $order_model = new \app\index\model\Order();
                $info = $order_model::get(['sn' => $param['out_trade_no']]);
                if ($info && $info->state == 1) {
                    $user = Member::get($info['uid']);

                    $res = $order_model->other_pay([],$info['id'],$user,time());
                    if ($res['code'] != 1) {
                        Put::write($route, '回调失败，订单号:' . $info->sn . $res['msg']);
                        return 'success';
                    }
                    $info->pay_state = 2;
                    $info->state = 2;
                    $info->pay_time = time();
                    $info->save();
                    Put::write($route, '成功，订单号:' . $info->sn);
                } else {
                    Put::write($route, '未查询到订单');
                }

                return 'success';
            } else {
                return 'fail';
            }
        } catch (\Exception $e) {
            return 'fail';

        }
    }

}
